# Variables for Unpacking from the tinycore ISO
SOURCE_ISO = tinycore_1.2.iso
KERNEL_UNPACK_FILENAME = bzImage
INITRD_UNPACK_FILENAME = tinycore.gz
ISOLINUX_DIRNAME = isolinux

# Variables for working version
WORK_DIR = working

# At the moment the bootloader files just get copied around via their directory name
ISOLINUX_DIR = $(WORK_DIR)/$(ISOLINUX_DIRNAME)

# NOTE: At the moment we just treat the kernel as a big binary blob.
# Once we start to modify and build it, things will need to change.
KERNEL_DIR = $(WORK_DIR)/kernel
KERNEL_FILENAME = bzImage
KERNEL_BUILD = $(WORK_DIR)/$(KERNEL_FILENAME)

INITRD_DIR = $(WORK_DIR)/initrd
# TODO change the filename to be tc-squared something
INITRD_FILENAME = tinycore.gz
INITRD_BUILD = $(WORK_DIR)/$(INITRD_FILENAME)

# Variables for packing and ISO creation
ISO_PACK = tc-squared.iso
PACK_DIR = .pack
KERNEL_PACK = $(PACK_DIR)/boot/$(KERNEL_FILENAME)
INITRD_PACK = $(PACK_DIR)/boot/$(INITRD_FILENAME)
ISOLINUX_PACK = $(PACK_DIR)/boot/isolinux

#variables for installing on THIS machine, as an alternative boot option
LOCAL_INSTALL_DIR = /boot/tc-squared

################################## TARGETS ##############################
all:	kernel initrd usage

usage:
	@echo "---------------------------------------------------------------------------"
	@echo "Available targets to 'make' are: unpack kernel initrd iso clean & install"
	@echo "Default target ('all') builds: kernel & initrd"
	@echo "Apply change sets to \"./working/\" using 'sudo bin/addcset <cset-directory>'" 
	@echo "Change sets you can apply are:" 
	@find . -maxdepth 1 -type d -name \*-cs

unpack: $(WORK_DIR)/newlyUnpacked usage

iso:	$(ISO_PACK)

kernel: $(KERNEL_BUILD)

initrd: $(INITRD_BUILD)

################################### UNPACK #############################
$(WORK_DIR)/newlyUnpacked: $(SOURCE_ISO)
	@echo "Mounting "$(SOURCE_ISO)" in .tmp"
	@mkdir -p .tmp
#	@sudo umount -f .tmp   # trap failures and unmount .tmp
	@sudo mount $(SOURCE_ISO) .tmp -o loop,ro
	@echo "Created working directory\t\t-->" $(WORK_DIR)/
	@echo "Copying contents of ISO \t\t-->" $(WORK_DIR)/
	@sudo rm -rf $(WORK_DIR)
	@mkdir -p $(WORK_DIR)
	@cp -R .tmp/boot/* $(WORK_DIR)
	@sudo umount .tmp
	@rmdir .tmp

#make sure we can clean up later
	@chmod +w -R $(ISOLINUX_DIR)

# make the kernel directory although it will be empty for now
	@mkdir -p $(KERNEL_DIR)

#make sure we can clean up later
	@chmod +w $(KERNEL_BUILD)
	@chmod +w $(INITRD_BUILD)

#unpack the initrd image into a sub directory so csets can be applied
	@mkdir -p $(INITRD_DIR)
	@echo "Extracting initrd filesystem to\t\t--> "$(INITRD_DIR)
# TODO these next three commands should go at the end but they are moved here
# as never gets to end as cpio always returns a failure....
	@echo "Unpacked from "$(SOURCE_ISO) > $(WORK_DIR)/log
	@echo "$(SOURCE_ISO) has been unpacked \t-->" $(WORK_DIR)

# TODO any flag for cpio to avoid the error "premature end of file"
# 'sudo' needed to preserve root ownership of some files
	@cd $(INITRD_DIR);sudo sh -c "zcat ../$(INITRD_UNPACK_FILENAME) | cpio -i -H newc -d"

################################### BUILD KERNEL #######################
$(KERNEL_BUILD): $(WORK_DIR)/log
# Fake, no build for now.
	@echo "Building kernel \t\t\t-->" $@
	@touch $@

################################### BUILD INITRD #######################
# TODO how to make this dependant on all the files under initrd??
$(INITRD_BUILD): $(INITRD_DIR)/*
	@echo "Building initrd ram disk image \t\t-->" $@
# jump into the working directory and create the compressed archive
# some files maybe not findable/readable if we are not root, hence 'sudo'
	@cd $(INITRD_DIR);sudo find | sudo cpio -o --quiet -H newc | gzip -2 > ../../$@
	@echo "Recompressing initrd image"
	@advdef -z4 $@

################################### CLEAN #############################
clean:
# Delete everything that is generated and not under version control!
	@echo "Removing $(WORK_DIR)/"
# some of the cpio unpacked files in working are owned by root
	@sudo rm -rf $(WORK_DIR)
	@rm -rf $(PACK_DIR)
	@echo "Removing "$(ISO_PACK)
	@rm -f $(ISO_PACK)

################################### PACK #############################
$(ISO_PACK):	$(KERNEL_PACK) $(INITRD_PACK) $(ISOLINUX_PACK)
	@echo "Change log:"
	@cat $(WORK_DIR)/log
	@mkisofs -quiet -l -J -V TC-custom -no-emul-boot -boot-load-size 4 -boot-info-table -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -o $@ $(PACK_DIR) 2>&1
	@echo "Pack: ISO created\t\t\t-->" $@

$(KERNEL_PACK): $(KERNEL_BUILD)
# copy in a (maybe) modified rebuilt kernel image
	@mkdir -p $(PACK_DIR)/boot
	@cp $(KERNEL_BUILD) $@

$(ISOLINUX_PACK): $(ISOLINUX_DIR)/*
# copy in a (maybe) modified isolinux etc
	@mkdir -p $(PACK_DIR)/boot
	@cp -R $(ISOLINUX_DIR) $(PACK_DIR)/boot/

$(INITRD_PACK): $(INITRD_BUILD)
# copy in a (maybe) modified root file system to boot from
	@mkdir -p $(PACK_DIR)/boot
	@cp -f $(INITRD_BUILD) $@

install: kernel initrd
	@echo If this fails, it´s probably because you don´t have permissions to write in /boot
	@echo Execute as root, or using "sudo" to solve that problem.
	@mkdir -p $(LOCAL_INSTALL_DIR)
	cp -f $(INITRD_BUILD) $(LOCAL_INSTALL_DIR)/
	cp -f $(KERNEL_BUILD) $(LOCAL_INSTALL_DIR)/
	@echo "Make sure you have an entry in your boot loader file (e.g. menu.lst for GRUB) like this:"
	@echo "-----------------------cut here-----------------------------------------------"
	@echo "title tc-squared"
	@echo "kernel $(LOCAL_INSTALL_DIR)/`basename $(KERNEL_BUILD)` silent console=ttyS0"
	@echo "append initrd=$(LOCAL_INSTALL_DIR)/`basename $(INITRD_BUILD)` quiet ssh console=ttyS0 splash=silent vga=0x314"
	@echo "console 0"
	@echo "implicit 0"	
	@echo "prompt 0"
	@echo "-----------------------cut here-----------------------------------------------"
